package com.eclipse.training.extensions.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ui.PlatformUI;

import com.eclipse.training.extensions.Activator;
import com.eclipse.training.extensions.editors.TrainingPageEditor;
import com.eclipse.training.models.BaseModelElement;

public class RemAction extends Action {
	boolean isContainer = false;

	public RemAction(boolean isContainer) {
		this.isContainer = isContainer;

		setImageDescriptor(Activator.getDefault().getImageDescriptor(
				Activator.IMAGE_REM));
		if (isContainer) {
			setText(Messages.RemAction_0);
			setToolTipText(Messages.RemAction_1);
		} else {
			setText(Messages.RemAction_2);
			setToolTipText(Messages.RemAction_3);
		}
	}

	@Override
	public void run() {
		
		/*
		 * Get the selection from the active editor
		 */
		TrainingPageEditor editor = (TrainingPageEditor) PlatformUI
				.getWorkbench().getActiveWorkbenchWindow().getActivePage()
				.getActiveEditor();
		TreeViewer viewer = editor.getViewer();
		ITreeSelection sel = (ITreeSelection) viewer.getSelection();
		BaseModelElement element = (BaseModelElement) sel.getFirstElement();

		/*
		 * Update the model
		 */
		editor.getModel().remove(element);

		/*
		 * Refresh the viewer and set the editor as dirty
		 */
		viewer.refresh(true);
		editor.treeModified();

		super.run();
	}
}
